package oceanwiki.controller;

import oceanwiki.req.EbookQueryReq;
import oceanwiki.req.EbookSaveReq;
import oceanwiki.resp.CommonResp;
import oceanwiki.resp.EbookQueryResp;
import oceanwiki.resp.PageResp;
import oceanwiki.service.IEbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 电子书 前端控制器
 * </p>
 *
 * @author XBJ
 * @since 2024-09-07
 */
@RestController
@RequestMapping("/ebook")
public class EbookController {

    @Autowired
    private IEbookService ebookService;

    @GetMapping(value = "/list")
    public CommonResp list(@Valid EbookQueryReq req) {
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp(true, "查询成功", null);

        PageResp<EbookQueryResp> pageResp = ebookService.listByName(req);
        resp.setContent(pageResp);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody EbookSaveReq req) {
        CommonResp resp = new CommonResp<>(true, "成功", null);

        ebookService.save(req);
        return resp;
    }

    //删除
    @DeleteMapping("/remove/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();

        this.ebookService.delete(id);
        return resp;
    }
}
